package com.example.anydo.net;

import android.app.Activity;

import android.widget.Toast;

import com.example.anydo.bean.LoginBackBean;
import com.example.anydo.constant.Url;


public class NetExample {

    Activity activity;
    public NetExample( Activity activity){
        this.activity = activity;
    }
    private String TAG = "OkHttp";
    String url = Url.BASE_URL + "login";

    // 网络请求示例
    public void example(){
        NetClient.getInstance() //获取单例
                .withUrl(url)  // 添加url
                .activity(activity)  // 传入activity
                .addParams("name","jizhangxiong1")  // 添加第一个参数
                .addParams("password","123456")  // 添加第二个参数
                .asObject(LoginBackBean.class)  // 设置返回数据类型
                .doGet(new DataBack<LoginBackBean>() {   // 设置返回回调
                    @Override
                    public void onResponse(LoginBackBean data) {  // 请求成功回调
                        // 请求成功
                        if(data.getCode() == 200){ // 登录成功

                            Toast.makeText(activity,"用户"+data.getData().getId()+data.getMessage(), Toast.LENGTH_SHORT).show();
                        }else {  // 登录失败

                            Toast.makeText(activity,data.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(String msg) {  // 请求失败回调
                        // 请求失败
                        Toast.makeText(activity,"请求失败",Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
